import axios from "axios";
import {deserialize} from "deserialize-json-api";

export const authInstance = axios.create({
    baseURL: process.env.REACT_APP_AUTH_API_BASE_URL,
    headers: {
        'Content-Type': 'application/json; charset=utf-8',
        'Pragma': 'no-cache'
    }
});

export const instance = axios.create({
    baseURL: process.env.REACT_APP_API_BASE_URL,
    headers: {
        'Content-Type': 'application/json; charset=utf-8',
        'Pragma': 'no-cache'
    }
});

// REQUEST INTERCEPTOR
instance.interceptors.request.use(req => {
    if(process.env.NODE_ENV === 'development') {
        console.log(`Request: ${JSON.stringify(req)}`);
    }
    return req;
}, error => Promise.reject(error));

// RESPONSE INTERCEPTOR
instance.interceptors.response.use(res => {
    const deserializedRes = deserialize(res.data);
    if(process.env.NODE_ENV === 'development') {
        console.log(`Request: ${JSON.stringify(deserializedRes)}`);
    }
    return deserializedRes;
}, error => Promise.reject(error));
